package com.wmx.controller;

import com.wmx.event.CustomEvent;
import com.wmx.event.MessageEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotEmpty;

/**********************************
 * @author wmx
 * @date 2019/9/25 10:19
 * @description:
 * @version 1.0
 ************************************/
@Slf4j
@Validated
@RestController
@RequestMapping("/event")
public class EventController {
    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;

    @GetMapping("/publishEvent")
    public String publishEvent(@NotEmpty(message = "请求编码不能为空") String code , @NotEmpty(message = "请求消息不能为空") String message){
        log.info("发布applicationEvent事件:{},{}", code, message);
        applicationEventPublisher.publishEvent(new CustomEvent(this, MessageEntity.builder().code(code).message(message).build()));
        log.info("发布事件成功");
        return "success";
    }
}
